<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
    	if($_POST){
           $userModel = D('user');
           if($userModel->create()){
           	 if($userModel->login()){
           	 	$this->success('登录成功！正在跳转...',U('Index/index'));
           	 	exit;
           	 }
           }
           $this->error($userModel->getError());
    	}
    	$this->display();
    }

   //注册页面
    public function register(){
    	if($_POST){
    		$userModel = D('user');
    		if($userModel->create()){
    			if($userModel->add()){
    				$this->success('注册成功，快去登录！',U('Login/index'));
    				exit;
    			}
    		}
    		$this->success('注册失败！',U('Login/register'));
    	}
    	//取出部门分组信息
    	$departmentModel = D('department');
    	$departmentInfo = $departmentModel->select();
    	$this->assign(array(
    		'department' =>$departmentInfo , 
    		));
    	$this->display();
    }
    //取出注册选择部门后选择分组
    public function group(){
    	$departId = I('post.departId');
    	$groupModel = D('group');
    	$groupInfo = $groupModel->where(array('did' =>$departId , ))->select();
    	echo json_encode(array(
    		'groups' =>$groupInfo , 
    		));
    }
    //退出
    public function out(){
    	//清空session
    	session(null);
    	//跳转
    	redirect(U('index')); 
    }
}